/**
 * \file driver/keyboard/keyboard.c
 * \author Davide Gessa
 * \date 10-13-09
 */
/*
 * Server name: keyboard
 * 
 * Server functions:
 * 	KEYBOARD_GETCHAR	:	Return a char
 * 		Reply:
 * 			1 		: 	Readed haracter
 *  KEYBOARD_SETCHARSET	:	Set a registed charset
 * 		Req:
 * 			1/32	:	Charset name
 *  KEYBOARD_ADDCHARSET	:	Add a custom charset
 * 		Req:
 * 			1/32	:	Charset name
 * 			32/300	:	Characters
 *  KEYBOARD_GETCHARSET	:	Get a charset from name
 * 		Req:
 * 			1/32	:	Charset name
 * 		Reply:
 * 			1/32	:	Charset name
 * 			32/300	:	Characters	
 */

#include <ctype.h>
#include <system/syscall.h>
#include "keyboard.h"


/**
 * Caratteri ascii
 */
char keyboard_standard_charset[] = {
		0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\'', 0, 0, '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',   
		0, 0, '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0, 0, 0, 0, 0, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', 
		'-', 0, 0, 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'-', 0, '5', 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
};


			
/**
 * Caratteri ascii maiuscoli
 */
char keyboard_standard_charset_shifted[] = {
		0, 0, '!', '"', 0, '$', '%', '&', '/', '(', ')', '=', '?', '^', 0, '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[',
		']', '\n', 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '@', '#', 0, 0, '\\', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ';', ':', '_', 0,
		0, 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'-', 0, '5', 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
};


/*
 * Handler della tastiera
 */
/*static void keyboard_callback(regs_t *reg)
{	
	unsigned code = io_inb(0x60);

	// Tasto rilasciato
    if (code & 0x80)
    {
		code &= 0x7F;
		switch(code)
		{
			case SHIFT_LEFT:
			case SHIFT_RIGHT:
				shift = 0;
				break;
		}
    }
    // Tasto premuto
    else
    {
		switch(code)
		{
			case PAGE_DOWN:
				break;
				
			case PAGE_UP:
				break;
				
			case LOCK_CAPS:
				capslock = !capslock;
				keyboard_update_leds(LED_CAPS | LED_NUM);
				break;
				
			case LOCK_NUM:
				numlock = !numlock;
				keyboard_update_leds(LED_CAPS | LED_NUM);
				break;
				
			case SHIFT_LEFT:
			case SHIFT_RIGHT:
				shift = 1;
				break;
			
			default:
				if((shift && !capslock) || (!shift && capslock)) buf[bufpoint] = keyboard_standard_charset_shifted[code];
				else buf[bufpoint] = keyboard_standard_charset[code];
				
				bufsize++;
				bufpoint++;
				break;
		}
    }
}
*/

/**
 * Aggiorna lo stato dei led della tastiera
 * 
 * \param status flag dei led da abilitare/disabilitare
 */
void keyboard_update_leds(char status)
{
 	char tmp;
 	/*while((io_inb(0x64)&2)!=0){}
 	io_outb(0x60,0xED);
 
 	while((io_inb(0x64)&2)!=0){}
 	io_outb(0x60,status);*/
 }



/**
 * Server della tastiera
 */
int main(int argc, char **argv)
{
	/*
	message_t message;
	
	// Il servizio si chiama keyboard
	srv_create("keyboard");
	
	// Installa l'irq callback
	hw_handler_reg(1, keyboard_callback); 
	
	// Handler dei messaggi
	while(1)
	{
		if(message_recv(&message) == 0) continue;

		switch(data[0])
		{
			case KEYBOARD_GETCHAR:
				message.data[0] = keyboard_getchar();
				message.data[1] = 0;
				message.size = 1;
				message_send(message);
				break;			
		}
	}		

	srv_destroy("keyboard");
	*/
	return 0;
}
